package com.dingreading.cloud.mall.api;

import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.EnumType;
import com.dingreading.cloud.mall.entity.OrderForm;
import com.dingreading.cloud.mall.service.OrderFormService;
import com.dingreading.cloud.mall.util.PayDealV3Util;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/payOk")
public class PayOkApiController {

    @Resource
    private OrderFormService orderFormService;

    @ApiOperation(value = "赠品订单-支付成功处理")
    @GetMapping(value = "/giftGoodsOrder", produces = "application/json; charset=UTF-8")
    public String giftGoodsOrder(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "订单号") @RequestParam(value = "orderCode") String orderCode,
            @ApiParam(value = "备注") @RequestParam(value = "remark") String remark
    ) throws Exception {
        OrderForm form = orderFormService.getByCode(null, orderCode);
        if (form == null)
            return "订单获取失败";

        String msg = PayDealV3Util.payOkGiftGoodsOrder(
                headStaffUid, headStaffName, form,
                EnumType.PayType.GIFT.getType(), EnumType.TradeType.GIFT.getType(), EnumType.PayChannelCode.GIFT.getType(), remark
        );
        return msg;

    }

    @ApiOperation(value = "自营门店订单-支付成功处理")
    @GetMapping(value = "/selfGoodsOrder", produces = "application/json; charset=UTF-8")
    public String selfGoodsOrder(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "订单号") @RequestParam(value = "orderCode") String orderCode,
            @ApiParam(value = "备注") @RequestParam(value = "remark") String remark
    ) throws Exception {
        OrderForm form = orderFormService.getByCode(null, orderCode);
        if (form == null)
            return "订单获取失败";

        String msg = PayDealV3Util.payOkSelfGoodsOrder(
                headStaffUid, headStaffName, form,
                EnumType.PayType.SELF.getType(), EnumType.TradeType.SELF.getType(), EnumType.TradeType.SELF.getName(),
                "", "", remark
        );
        return msg;
    }

    @ApiOperation(value = "加盟门店银行转账订单-支付成功处理")
    @GetMapping(value = "/bankGoodsOrder", produces = "application/json; charset=UTF-8")
    public String bankGoodsOrder(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "订单号") @RequestParam(value = "orderCode") String orderCode,
            @ApiParam(value = "支付时间") @RequestParam(value = "payTime") String payTime,
            @ApiParam(value = "备注") @RequestParam(value = "remark") String remark
    ) throws Exception {
        R<Object> r = PayDealV3Util.payOkGoodsOrder(
                orderCode, EnumType.PayType.BANK.getType(), EnumType.TradeType.BANK.getType(),
                EnumType.TradeType.BANK.getName(), "", "", payTime,
                headStaffUid, headStaffName, remark, 2
        );

        if (R.isError(r))
            return r.getMsg();

        return "";

    }

}
